package com.meeting.project.business.record.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.meeting.common.exception.ServiceException;
import com.meeting.common.utils.DateUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meeting.project.business.record.domain.SeriesData;
import com.meeting.project.business.record.mapper.SignInRecordMapper;
import com.meeting.project.business.record.domain.SignInRecord;
import com.meeting.project.business.record.service.ISignInRecordService;
import com.meeting.common.utils.text.Convert;

/**
 * 人员签到信息Service业务层处理
 *
 * @author llmaoa
 * @date 2025-08-05
 */
@Service
public class SignInRecordServiceImpl implements ISignInRecordService {
    @Autowired
    private SignInRecordMapper signInRecordMapper;

    // 允许重复签到的请假类型
    private static final List<Integer> LEAVE_TYPE_ALLOW_REPEAT_SING = new ArrayList<Integer>() {
        {
            add(0);
        }
    };


    /**
     * 查询人员签到信息
     *
     * @param id 人员签到信息主键
     * @return 人员签到信息
     */
    @Override
    public SignInRecord selectSignInRecordById(Long id) {
        return signInRecordMapper.selectSignInRecordById(id);
    }

    /**
     * 查询人员签到信息列表
     *
     * @param signInRecord 人员签到信息
     * @return 人员签到信息
     */
    @Override
    public List<SignInRecord> selectSignInRecordList(SignInRecord signInRecord) {
        return signInRecordMapper.selectSignInRecordList(signInRecord);
    }

    /**
     * 新增人员签到信息
     *
     * @param signInRecord 人员签到信息
     * @return 结果
     */
    @Override
    public int insertSignInRecord(SignInRecord signInRecord) {
        // 判断当前用户是否已经签过到
        SignInRecord oldSignInRecord = signInRecordMapper.selectPersonSignInRecordList(signInRecord.getMeetingId(), signInRecord.getRealname(), signInRecord.getPhone(), null);
        if (Objects.isNull(oldSignInRecord)) {
            signInRecord.setCreateTime(DateUtils.getNowDate());
            return signInRecordMapper.insertSignInRecord(signInRecord);
        } else {
            // 判断用户是否为门诊请假 否则提示用户已经签过到
            if (LEAVE_TYPE_ALLOW_REPEAT_SING.contains(oldSignInRecord.getLeaveType())) {
                oldSignInRecord.setAttendFlag(signInRecord.getAttendFlag());
                oldSignInRecord.setLeaveType(Objects.isNull(signInRecord.getLeaveType()) ? null : signInRecord.getLeaveType());
                oldSignInRecord.setLeaveReason(StringUtils.isBlank(signInRecord.getLeaveReason()) ? null : signInRecord.getLeaveReason());
                oldSignInRecord.setAttachment(StringUtils.isBlank(signInRecord.getAttachment()) ? null : signInRecord.getAttachment());
                oldSignInRecord.setRemark(StringUtils.isBlank(signInRecord.getRemark()) ? null : signInRecord.getRemark());
                oldSignInRecord.setUpdateTime(DateUtils.getNowDate());
                return signInRecordMapper.updateSignInRecord(oldSignInRecord);
            } else {
                throw new ServiceException("您已经签过到，请勿重复签到");
            }
        }
    }

    /**
     * 修改人员签到信息
     *
     * @param signInRecord 人员签到信息
     * @return 结果
     */
    @Override
    public int updateSignInRecord(SignInRecord signInRecord) {
        signInRecord.setUpdateTime(DateUtils.getNowDate());
        return signInRecordMapper.updateSignInRecord(signInRecord);
    }

    /**
     * 批量删除人员签到信息
     *
     * @param ids 需要删除的人员签到信息主键
     * @return 结果
     */
    @Override
    public int deleteSignInRecordByIds(String ids) {
        return signInRecordMapper.deleteSignInRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人员签到信息信息
     *
     * @param id 人员签到信息主键
     * @return 结果
     */
    @Override
    public int deleteSignInRecordById(Long id) {
        return signInRecordMapper.deleteSignInRecordById(id);
    }

    @Override
    public List<SeriesData> statisticsByLeaveType(Long meetingId) {
        return signInRecordMapper.statisticsByLeaveType(meetingId);
    }

    @Override
    public List<SeriesData> statisticsAllByLeaveType() {
        return signInRecordMapper.statisticsAllByLeaveType();
    }

    @Override
    public List<SignInRecord> selectSignInRecordListByMeetingIds(List<Long> meetingIds) {
        return signInRecordMapper.selectSignInRecordListByMeetingIds(meetingIds);
    }
}
